<template>
  <el-dialog
    title="卡券密钥查看"
    :close-on-click-modal="false"
    @close="closeHandle"
    :visible.sync="visible"
    append-to-body>
    <el-form :model="dataForm" :rules="tempRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item prop="captcha">
        <el-row :gutter="20">
          <el-col :span="14">
            <el-input v-model="dataForm.captcha" placeholder="验证码">
            </el-input>
          </el-col>
          <el-col :span="10" class="login-captcha">
            <img :src="captchaPath" @click="getCaptcha()" alt=""
                 style="height: 32px; width: 96px; border-radius: 4px;">
          </el-col>
        </el-row>
      </el-form-item>

      <el-form-item prop="smsCode">
        <el-col :span="14">
          <el-input v-model="dataForm.smsCode" placeholder="短信验证码"></el-input>
        </el-col>
        <el-col :span="10">
          <el-button @click="sendSMS()" :disabled="disabled" v-model="sendSMSBtnText">{{sendSMSBtnText}}</el-button>
        </el-col>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="dataFormSubmit()" style="width: 100%">提交</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import {getUUID} from '@/utils'

export default {
  name: "user-crad-sn-verification",
  data() {
    return {
      countDown: {
        type: Number,
        default: 60
      },
      myHeaders: {},
      sendSMSBtnText: '发送短信',
      disabled: false,
      visible: false,
      captchaPath: '',
      dataForm: {
        captcha: '',
        uuid: '',
        smsCode: ''
      },
      dataRule: {
        smsCode: [
          {
            required: true,
            message: '短信验证码不能为空',
            trigger: 'blur'
          }
        ]
      },
      tempRule: {},
      dataRule1: {
        captcha: [
          {
            required: true,
            message: '图片验证码不能为空',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    init() {
      this.visible = true
      this.tempRule = this.dataRule1
      this.getCaptcha()
    },
    dataFormSubmit() {
      this.tempRule = this.dataRule
      this.$refs['dataForm']
        .validate((valid) => {
          if (valid) {
            this.$http({
              url: `/mall/usercard/verificationCardSn/`,
              method: 'post',
              data: {
                'code': this.dataForm.smsCode
              }
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500
                })
                this.visible = false
              }
            })
          }
        })
    },//获取短信验证码
    sendSMS() {
      this.tempRule = this.dataRule1
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            async: false,
            url: '/mall/usercard/sendSms',
            method: 'post',
            data: {
              'uuid': this.dataForm.uuid,
              'code': this.dataForm.captcha,
              'configId': "note_code",
            }
          }).then(({data}) => {
            if (data && data.code === 0) {
              console.log("短信发送成功")
            } else {
              // this.$message({
              //   message: data.msg,
              //   type: 'success',
              //   duration: 1500
              // })
              this.getCaptcha()
            }
          })
          let timer = setInterval(() => {
            this.time--;
            this.sendSMSBtnText = `${this.time}s后重新发送`
            if (this.time == 0) {
              this.sendSMSBtnText = `重新发送`
            }
            this.disabled = true
            if (this.time === 0) {
              this.disabled = false;
              this.btnText = '重新发送';
              this.time = this.countDown.default
              clearInterval(timer)
            }
          }, 1000)
          this.$emit('send')
        }
      })
    },
    // 获取验证码
    getCaptcha() {
      this.dataForm.uuid = getUUID()
      this.captchaPath = this.$http.BASE_URL + `/mall/usercard/captcha.jpg?uuid=${this.dataForm.uuid}`
    },
    closeHandle() {
      this.fileList = []
      this.$emit('refreshDataList')
    }
  },
  mounted() {
    this.time = this.countDown.default
  }
}


</script>

<style scoped>

</style>
